package com.creditcmsplt.common.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.creditcmsplt.common.CommonPhpService;
import com.creditcmsplt.entity.PhpResult;
import com.creditcmsplt.entity.param.BCreditbiRspData;
import com.creditcmsplt.entity.param.CiUpFileReq;
import com.creditcmsplt.entity.param.CiUpFileReqData;
import com.creditcmsplt.entity.param.SncfDownloadReq;
import com.creditcmsplt.entity.param.SncfDownloadReqData;
import com.creditcmsplt.entity.param.SncfStatusReq;
import com.creditcmsplt.entity.param.SncfStatusReqData;
import com.creditcmsplt.entity.param.SncfUpIdentifyReq;
import com.creditcmsplt.entity.param.SncfUpIdentifyReqData;
import com.creditcmsplt.entity.param.SncfUpSignReq;
import com.creditcmsplt.entity.param.SncfUpSignReqData;
import com.creditcmsplt.entity.param.SncfUpUserInfoReq;
import com.creditcmsplt.entity.param.SncfUpUserInfoReqData;
import com.creditcmsplt.properties.CollectInfoProperties;
import com.creditcmsplt.properties.SnCollectInfoProperties;
import com.creditcmsplt.util.Base64Util;
import com.creditcmsplt.util.FileUtil;
import com.creditcmsplt.util.HttpUtil;
import com.creditcmsplt.util.TextUtils;
import com.creditcmsplt.util.UUIDUtil;

@Service
public class CommonPhpServiceImpl implements CommonPhpService{

	private final Logger log = LoggerFactory.getLogger(CommonPhpServiceImpl.class);
	
	@Autowired
	private CollectInfoProperties collectInfoProperties;
	
	@Autowired
	private SnCollectInfoProperties snCollectInfoProperties;
	
	@Override
	public String getLogin(String userid,String password) {
		Map<String, Object> formparam = new HashMap<String, Object>();
		formparam.put("username", userid);
		formparam.put("password", password);
		String result = HttpUtil.execuateHttpFile(collectInfoProperties.getLoginurl(), "post",formparam, null);// 第一步调用login接口
		JSONObject apiJson = (JSONObject) JSON.parse(result);// error=0位成功获取
		return apiJson==null?null:apiJson.getString("token");
	}

	@Override
	public String getToken(String loginToken) {
		Map<String, Object> formparam = new HashMap<String, Object>();
		formparam.put("token", loginToken);
		String result = HttpUtil.execuateHttpFile(collectInfoProperties.getTokenurl(), "post", formparam,
				null);// 第二部调用token接口
		JSONObject apiJson = (JSONObject) JSON.parse(result);// error=0位成功获取
		return apiJson==null?null:apiJson.getString("token");
	}

	@Override
	public PhpResult uploadIdentify(CiUpFileReq infoParam, String apitoken, Long cid) {
		CiUpFileReqData data=infoParam.getData();
		Map<String, Object> formparam = new HashMap<String, Object>();
		formparam.put("type", "1");
		formparam.put("ctrl", "uploadFile");
		formparam.put("token", apitoken);
		formparam.put("cid", cid);
		formparam.put("queryid", data.getAuthorityNumber());
		// 图片
		String identify_name=UUIDUtil.getUUIDUnline();
		String identify_back_name=UUIDUtil.getUUIDUnline();
		String identifyPath = collectInfoProperties.getImagesPath() + identify_name + "."+ data.getIdentify_type();
		Base64Util.bs64stringToFile(data.getIdentify(), identifyPath);// 身份证正面
		String identify_backPath = collectInfoProperties.getImagesPath() + identify_back_name+ "." + data.getIdentify_back_type();
		Base64Util.bs64stringToFile(data.getIdentify_back(), identify_backPath);// 身份证反面

		Map<String, String> files = new HashMap<String, String>();
		files.put("identify.jpg", identifyPath);
		files.put("identify_back.jpg", identify_backPath);
		String result = HttpUtil.execuateHttpFile(collectInfoProperties.getUploadIdentifyImage(), "post",formparam, files);// 身份证正反面
		FileUtil.deleteFiles(collectInfoProperties.getImagesPath() + identify_name + "."+ data.getIdentify_type(),
				collectInfoProperties.getImagesPath() + identify_back_name + "."+ data.getIdentify_back_type());
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}

	@Override
	public PhpResult uploadOtherFile(CiUpFileReq infoParam, String apitoken, String cid) {
		CiUpFileReqData data=infoParam.getData();
		Map<String, Object> formparam = new HashMap<String, Object>();
		Map<String, String> files = new HashMap<String, String>();
		formparam.put("type", "2");
		formparam.put("ctrl", "uploadFile");
		formparam.put("cid", cid);
		formparam.put("token", apitoken);
		formparam.put("queryid", data.getAuthorityNumber());
		String authority_name=UUIDUtil.getUUIDUnline();
		String applyfor_name=UUIDUtil.getUUIDUnline(); 
		String handholdidentify_name=UUIDUtil.getUUIDUnline(); 
		String authorityPath = collectInfoProperties.getImagesPath() + authority_name + "."+ data.getAuthority_type();
		Base64Util.bs64stringToFile(data.getAuthority(), authorityPath);// 授权书图
		String applyForPath = collectInfoProperties.getImagesPath() + applyfor_name + "."+ data.getApplyFor_type();
		Base64Util.bs64stringToFile(data.getApplyFor(), applyForPath);// 申请书
		String handHoldIdentifPath = collectInfoProperties.getImagesPath() + handholdidentify_name+ "." + data.getHandHoldIdentify_type();
		Base64Util.bs64stringToFile(data.getHandHoldIdentify(), handHoldIdentifPath);// 手持身份证照
		files.put("authority.jpg", authorityPath);
		files.put("applyFor.jpg", applyForPath);
		files.put("mixCard.jpg", handHoldIdentifPath);
		String result = HttpUtil.execuateHttpFile(collectInfoProperties.getUploadOtherImage(), "post",formparam, files);// 其余授权书等图片
		
		FileUtil.deleteFiles(collectInfoProperties.getImagesPath() + authority_name + "."+ data.getAuthority_type(),
				collectInfoProperties.getImagesPath() + applyfor_name + "."+ data.getApplyFor_type(),
				collectInfoProperties.getImagesPath() + handholdidentify_name + "."+ data.getHandHoldIdentify_type());
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}

	@Override
	public PhpResult uploadInfo(CiUpFileReq infoParam, String cid, String apitoken, String userid) {
		CiUpFileReqData data=infoParam.getData();
		Map<String, Object> formparam = new HashMap<String, Object>();
		formparam.put("ctrl", "userinfo");
		formparam.put("cid", cid);
		formparam.put("token", apitoken);
		String exceinfo = data.getAuthorityNumber() + ";"
				+ data.getUserName() + ";" + data.getGender() + ";"
				+ data.getNational() + ";" + data.getAddress() + ";"
				+ data.getIdentifyno() + ";"
				+ data.getIssuingAuthority() + ";"
				+ data.getCertificateExpire() + ";"
				+ data.getPhoneNumber() + ";" + 9999;//9999邮政编码
		formparam.put("exceinfo", exceinfo);
		formparam.put("check", "");
		formparam.put("tel", userid);
		String result = HttpUtil.execuateHttpFile(collectInfoProperties.getUploadUserInfo(), "post",
				formparam, null);// 第三用户上传资料
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}

	@Override
	public PhpResult queryStatus(String token, String identify) {
		Map<String, Object> formparam = new HashMap<String, Object>();
		formparam.put("token", token);
		formparam.put("uid", identify);
		String resultStr=HttpUtil.execuateHttpFile(collectInfoProperties.getQueryApi(), "post",formparam, null);// 第一步调用login接口
		return JSONObject.toJavaObject((JSONObject)JSON.parse(resultStr), PhpResult.class);
	}
	
	@Override
	public PhpResult appAuthority(String apitoken, Integer num,String credit_id) {
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("num", num);
		paramsMap.put("user_id", credit_id);
		String result=HttpUtil.execuateHttpFile(collectInfoProperties.getAuthorPdfApply(), "post", paramsMap, null);
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}
	
	@Override
	public PhpResult authorPdfDown(String apitoken, String applyid) {
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("applyid", applyid);
		String result=HttpUtil.execuateHttpFile(collectInfoProperties.getAuthorPdfDown(), "post", paramsMap, null);
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}
	
	@Override
	public void changeStatus(String apitoken, String applyid) {
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("applyid", applyid);
		HttpUtil.execuateHttpFile(collectInfoProperties.getChangeStatusApi(), "post", paramsMap, null);
	}

	@Override
	public PhpResult snUpIdentify(SncfUpIdentifyReq params,String apitoken) {
		SncfUpIdentifyReqData dataParam=params.getData();
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("id_num", dataParam.getIdentifyno());
		paramsMap.put("is_machine", dataParam.getIsMachine());
		
		String identify = collectInfoProperties.getImagesPath() + UUIDUtil.getUUIDUnline()+ ".jpg";
		Base64Util.bs64stringToFile(dataParam.getIdentify(), identify);// 身份证正面
		
		String identify_back = collectInfoProperties.getImagesPath() + UUIDUtil.getUUIDUnline()+ ".jpg";
		Base64Util.bs64stringToFile(dataParam.getIdentify_back(), identify_back);// 身份证反面
		
		String avatar = collectInfoProperties.getImagesPath() + UUIDUtil.getUUIDUnline()+ ".jpg";
		Base64Util.bs64stringToFile(dataParam.getAvatar(), avatar);// 身份证图像照
		
		String handHoldIdentify = collectInfoProperties.getImagesPath() + UUIDUtil.getUUIDUnline()+ ".jpg";
		Base64Util.bs64stringToFile(dataParam.getHandHoldIdentify(), handHoldIdentify);// 手持证件照
		
		//String authority = collectInfoProperties.getImagesPath() + UUIDUtil.getUUIDUnline()+ ".jpg";
		//Base64Util.bs64stringToFile(dataParam.getAuthority(), authority);// 手持证件照
		
		Map<String,String> files=new HashMap<String, String>();
		files.put("idcard_front.jpg", identify);
		files.put("idcard_back.jpg", identify_back);
		files.put("mixCard.jpg", handHoldIdentify);
		files.put("avatar.jpg", avatar);
		//files.put("authority.jpg", authority);
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getUpidentify(), "post",paramsMap, files);// 其余授权书等图片
		
		FileUtil.deleteFiles(identify,identify_back,handHoldIdentify,avatar);
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}

	@Override
	public PhpResult snUpUserInfo(SncfUpUserInfoReq params,String apitoken) {
		SncfUpUserInfoReqData dataParam=params.getData();
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("cid", dataParam.getCid());
		paramsMap.put("name", dataParam.getUserName());
		paramsMap.put("sex", dataParam.getGender().equals("男")?"1":"2");
		paramsMap.put("nation", dataParam.getNational());
		paramsMap.put("birthday", dataParam.getBirthday());
		paramsMap.put("id_num", dataParam.getIdentifyno());
		paramsMap.put("period", dataParam.getCertificateExpire());
		paramsMap.put("issued", dataParam.getIssuingAuthority());
		paramsMap.put("phone", dataParam.getPhoneNumber());
		paramsMap.put("address", dataParam.getAddress());
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getUpUserInfo(), "post",paramsMap, null);// 其余授权书等图片
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}

	@Override
	public PhpResult snUpSign(SncfUpSignReq params,String apitoken) {
		SncfUpSignReqData dataParam=params.getData();
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("id_num", dataParam.getIdentifyno());
		paramsMap.put("cid", dataParam.getCid());
		
		String sign = collectInfoProperties.getImagesPath() + UUIDUtil.getUUIDUnline()+ ".png";
		Base64Util.bs64stringToFile(dataParam.getSign(), sign);// 手持证件照
		
		Map<String,String> files=new HashMap<String, String>();
		files.put("sign.png", sign);
		
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getUpSign(), "post",paramsMap, files);// 其余授权书等图片
		
		FileUtil.deleteFiles(sign);
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}

	@Override
	public PhpResult snDownload(SncfDownloadReq params,String apitoken,String userId) {
		SncfDownloadReqData dataParam=params.getData();
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("idcard", dataParam.getIdentifyno());
		paramsMap.put("user_id", userId);
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getDownload(), "post",paramsMap, null);// 其余授权书等图片
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}

	@Override
	public PhpResult snQueryStatus(SncfStatusReq params,String apitoken,String userId) {
		SncfStatusReqData dataParam=params.getData();
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("idcard", dataParam.getIdentifyno());
		paramsMap.put("user_id", userId);
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getQueryStatus(), "post",paramsMap, null);// 其余授权书等图片
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}
	
	@Override
	public PhpResult snAuthDown(String apitoken) {
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getAuthDownload(), "post",paramsMap, null);// 其余授权书等图片
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}
	
	@Override
	public PhpResult snSendMsg(String apitoken,String identifyNo) {
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("idcard", identifyNo);
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getSendMsg(), "post",paramsMap, null);// 其余授权书等图片
		return JSONObject.toJavaObject((JSONObject)JSON.parse(result), PhpResult.class);
	}
	
	@Override
	public BCreditbiRspData getUserInfo(String apitoken,String identifyNo) {
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		paramsMap.put("token", apitoken);
		paramsMap.put("idcard", identifyNo);
		String result = HttpUtil.execuateHttpFile(snCollectInfoProperties.getGetUserInfo(), "post",paramsMap, null);// 其余授权书等图片
		JSONObject jsonObject=(JSONObject)JSON.parse(result);
		jsonObject=jsonObject==null?new JSONObject():jsonObject;
		Integer code=jsonObject.getInteger("error")!=null&&jsonObject.getInteger("error").equals(0)?1:-1;
		String reason=TextUtils.isEmpty(jsonObject.getString("reason"))?"失败":jsonObject.getString("reason");
		String msg=code.equals(1)?"成功":reason;
		BCreditbiRspData resultObject=JSONObject.toJavaObject((JSONObject)JSON.parse(result), BCreditbiRspData.class);
		resultObject=resultObject==null?new BCreditbiRspData():resultObject;
		resultObject.setCode(code);
		resultObject.setMsg(msg);
		return resultObject;
	}
	
}
